package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.AddressBook;
import com.itheima.reggie.entity.Orders;
import com.itheima.reggie.service.AddressBookService;
import com.itheima.reggie.service.OrderService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.*;

import javax.smartcardio.ATR;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private AddressBookService addressBookService;


    /**
     * 用户下单
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据：{}", orders);
        orderService.submit(orders);
        return R.success("下单成功！");
    }

    /**
     * 订单数据查询(用户端)
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("userPage")
    public R<Page> page(int page, int pageSize){
        log.info("page: {}, pageSize: {}", page, pageSize);
        Page<Orders> pageInfo = new Page<>(page, pageSize);

        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Orders::getCheckoutTime);

        orderService.page(pageInfo);
        return R.success(pageInfo);
    }

    /**
     * 订单数据查询(管理员端)
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("page")
    public R<Page> page(int page, int pageSize, String  number, @DateTimeFormat Date beginTime,  @DateTimeFormat Date endTime){
        log.info("page:{},pageSize:{},number:{},beginTime:{},endTime:{}",page,pageSize,number,beginTime,endTime);
        Page<Orders> pageInfo = new Page<>(page, pageSize);

        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(number != null, Orders::getNumber, number);
        queryWrapper.orderByDesc(Orders::getOrderTime);

        orderService.page(pageInfo);

        List<Orders> orders = pageInfo.getRecords();
        orders = orders.stream().map((item) -> {
            Orders order = new Orders();
            BeanUtils.copyProperties(item, order);
            //查询地址数据
            Long addressBookId = item.getAddressBookId();
            AddressBook addressBook = addressBookService.getById(addressBookId);

            order.setUserName(addressBook.getConsignee());

            return order;

        }).collect(Collectors.toList());

        pageInfo.setRecords(orders);

        return R.success(pageInfo);
    }

}
